Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

379
Views
Why does [NaN].includes(NaN) return true in JavaScript?

I'm familiar with NaN being "weird" in JavaScript, i.e., NaN === NaN always returns false, as described here. So one should not make === comparisons to check for NaN, but use isNaN(..) instead.

So I was surprised to discover that

> [NaN].includes(NaN)
true

This seems inconsistent. Why have this behavior?

How does it even work? Does the includes method specifically check isNaN?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

According to MDN's document say that

Note: Technically speaking, includes() uses the sameValueZero algorithm to determine whether the given element is found.

const x = NaN, y = NaN;
console.log(x == y); // false                -> using ‘loose’ equality
console.log(x === y); // false               -> using ‘strict’ equality
console.log([x].indexOf(y)); // -1 (false)   -> using ‘strict’ equality
console.log(Object.is(x, y)); // true        -> using ‘Same-value’ equality
console.log([x].includes(y)); // true        -> using ‘Same-value-zero’ equality


More detailed explanation:

  1. Same-value-zero equality similar to same-value equality, but +0 and −0 are considered equal.
  2. Same-value equality is provided by the Object.is() method: The only difference between Object.is() and === is in their treatment of signed zeroes and NaNs.

enter image description here


Additional resources:

  • Which equals operator (== vs ===) should be used in JavaScript comparisons?
  • Array.prototype.includes vs. Array.prototype.indexOf
  • Are +0 and -0 the same?
over 4 years ago · Santiago Trujillo Report

0

The .includes() method uses SameValueZero algorithm for checking the equality of two values and it considers the NaN value to be equal to itself.

The SameValueZero algorithm is similar to SameValue, but the only difference is that the SameValueZero algorithm considers +0 and -0 to be equal.

The Object.is() method uses SameValue and it returns true for NaN.

console.log(Object.is(NaN, NaN));

The behavior of .includes() method is slightly different from the .indexOf() method; the .indexOf() method uses strict equality comparison to compare values and strict equality comparison doesn't consider NaN to be equal to itself.

console.log([NaN].indexOf(NaN));

Information about different equality checking algorithms can be found at MDN:

MDN - Equality comparisons and sameness

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!